using HKB.Common;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
namespace HKB.Web
{
	public class LabelBll
	{
		private Conn conn = null;
		public LabelBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public LabelBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetAllLabel()
		{
			return this.conn.GetDataTable("Select * From [HL_Label] Order By UpdateTime DESC, ID DESC");
		}
		public DataTable GetLabelByLabelID(int LabelID)
		{
			return this.conn.GetDataTable("Select * From [HL_Label] Where ID=" + LabelID);
		}
		public bool DeleteLabel(int ID)
		{
			return this.conn.Execute("DELETE From [HL_Label] Where ID=" + ID) >= 1;
		}
		public bool AddLabel(string Name, string Type, string Intro, string DataType, string CacheTime, string OutPutType, string ConnStr, string LabelTemplate, string LabelSqlString, string CountPageSqlStr, string LabelOutPutType, string LabelStaticOutPut, string ParmArray)
		{
			string value = this.MakeDefineContent(Name, Type, Intro, DataType, CacheTime, OutPutType, ConnStr, LabelTemplate, LabelSqlString, CountPageSqlStr, LabelOutPutType, LabelStaticOutPut, ParmArray);
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@Name", DbType.String, Name),
				this.conn.CreateParameter("@Type", DbType.String, Type),
				this.conn.CreateParameter("@Define", DbType.String, value),
				this.conn.CreateParameter("@Template", DbType.String, LabelTemplate),
				this.conn.CreateParameter("@UpdateTime", DbType.DateTime, DateTime.Now),
				this.conn.CreateParameter("@OutPutType", DbType.String, LabelOutPutType),
				this.conn.CreateParameter("@StaticOutPut", DbType.String, LabelStaticOutPut)
			};
			return this.conn.Execute("Insert Into [HL_Label](Name,Type,Define,Template,UpdateTime,OutPutType,StaticOutPut) Values(@Name,@Type,@Define,@Template,@UpdateTime,@OutPutType,@StaticOutPut)", parameters) == 1;
		}
		public bool UpdateLabel(int ID, string Name, string Type, string Intro, string DataType, string CacheTime, string OutPutType, string ConnStr, string LabelTemplate, string LabelSqlString, string EnableLabelPageCount, string CountPageSqlStr, string LabelOutPutType, string LabelStaticOutPut, string[,] ParmArray)
		{
			DataTable labelByLabelID = this.GetLabelByLabelID(ID);
			bool result;
			if (labelByLabelID.Rows.Count == 0)
			{
				result = false;
			}
			else
			{
				DataRow dataRow = labelByLabelID.Rows[0];
				if (Name == "")
				{
					Name = dataRow["Name"].ToString();
				}
				if (Type == "")
				{
					Type = dataRow["Type"].ToString();
				}
				if (LabelOutPutType == "")
				{
					LabelOutPutType = dataRow["OutPutType"].ToString();
				}
				if (LabelStaticOutPut == "")
				{
					LabelStaticOutPut = dataRow["StaticOutPut"].ToString();
				}
				if (LabelTemplate == "")
				{
					LabelTemplate = dataRow["Template"].ToString();
				}
				string value = this.UpdateDefine(dataRow["Define"].ToString(), Name, Type, Intro, DataType, CacheTime, OutPutType, ConnStr, LabelTemplate, LabelSqlString, EnableLabelPageCount, CountPageSqlStr, LabelOutPutType, LabelStaticOutPut, ParmArray);
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@Name", DbType.String, Name),
					this.conn.CreateParameter("@Type", DbType.String, Type),
					this.conn.CreateParameter("@Define", DbType.String, value),
					this.conn.CreateParameter("@Template", DbType.String, LabelTemplate),
					this.conn.CreateParameter("@UpdateTime", DbType.DateTime, DateTime.Now),
					this.conn.CreateParameter("@OutPutType", DbType.String, LabelOutPutType),
					this.conn.CreateParameter("@StaticOutPut", DbType.String, LabelStaticOutPut),
					this.conn.CreateParameter("@ID", DbType.Int32, ID)
				};
				result = (this.conn.Execute("Update [HL_Label] Set [Name] = @Name, [Type] = @Type, [Define] = @Define, [Template] = @Template, [UpdateTime] = @UpdateTime, [OutPutType] = @OutPutType, [StaticOutPut] = @StaticOutPut Where ([ID] = @ID)", parameters) == 1);
			}
			return result;
		}
		public DataTable GetLabelByLabelName(string LabelName)
		{
			IDbDataParameter dbDataParameter = this.conn.CreateParameter("@LabelName", DbType.String, LabelName);
			return this.conn.GetDataTable("Select * From [HL_Label] Where ([Name] = @LabelName)", new IDbDataParameter[]
			{
				dbDataParameter
			});
		}
		public DataTable GetLabelByNameType(string Name, string Type)
		{
			DataTable result;
			if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Type))
			{
				IDbDataParameter[] parameters = new IDbDataParameter[]
				{
					this.conn.CreateParameter("@LabelName", DbType.String, Name),
					this.conn.CreateParameter("@LabelType", DbType.String, Type)
				};
				result = this.conn.GetDataTable("Select * From [HL_Label] Where (([Name] = @LabelName) and ([Type] = @LabelType))", parameters);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public DataTable GetLabelType()
		{
			return this.conn.GetDataTable("SELECT  Distinct(Type) FROM [HL_Label]");
		}
		public DataTable GetLabelByType(string LabelType)
		{
			DataTable result;
			if (!string.IsNullOrEmpty(LabelType) && LabelType != "")
			{
				IDbDataParameter dbDataParameter = this.conn.CreateParameter("@Type", DbType.String, LabelType);
				result = this.conn.GetDataTable("Select * From [HL_Label] Where ([Type] = @Type)  order by  UpdateTime DESC, ID DESC", new IDbDataParameter[]
				{
					dbDataParameter
				});
			}
			else
			{
				result = this.GetAllLabel();
			}
			return result;
		}
		public DataTable GetParmDataTable(string DefineContent)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(DefineContent);
			XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/parms");
			string text = "";
			foreach (XmlNode xmlNode in xmlNodeList)
			{
				text = text + "<parms>" + xmlNode.InnerXml + "</parms>";
			}
			DataTable result;
			if (text != "")
			{
				DefineContent = "<?xml version=\"1.0\"?><root>" + text + "</root>";
				DataSet dataSet = LabelBll.ConvertXMLToDataSet(DefineContent);
				DataTable dataTable = dataSet.Tables[0];
				result = dataTable;
			}
			else
			{
				result = null;
			}
			return result;
		}
		public bool CopyLabelByID(int ID)
		{
			bool result;
			if (ID > 0)
			{
				SqlParameter sqlParameter = new SqlParameter("@ID", DbType.String);
				sqlParameter.Value = ID;
				int num = this.conn.Execute("PR_CopyLabel", new SqlParameter[]
				{
					sqlParameter
				});
				result = (num == 1);
			}
			else
			{
				result = false;
			}
			return result;
		}
		public DataTable GetLabelDefineByLabelID(int LabelID)
		{
			DataTable labelByLabelID = this.GetLabelByLabelID(LabelID);
			DataTable result;
			if (labelByLabelID.Rows.Count == 1)
			{
				DataSet dataSet = LabelBll.ConvertXMLToDataSet(labelByLabelID.Rows[0]["Define"].ToString());
				result = dataSet.Tables[0];
			}
			else
			{
				result = null;
			}
			return result;
		}
		public static DataSet ConvertXMLToDataSet(string xmlData)
		{
			XmlTextReader xmlTextReader = null;
			DataSet result;
			try
			{
				DataSet dataSet = new DataSet();
				StringReader input = new StringReader(xmlData);
				xmlTextReader = new XmlTextReader(input);
				dataSet.ReadXml(xmlTextReader);
				result = dataSet;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				if (xmlTextReader != null)
				{
					xmlTextReader.Close();
				}
			}
			return result;
		}
		public string MakeDefineContent(string Name, string Type, string Intro, string DataType, string CacheTime, string OutPutType, string ConnStr, string LabelTemplate, string LabelSqlString, string CountPageSqlStr, string LabelOutPutType, string LabelStaticOutPut, string ParmArray)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<?xml version=\"1.0\"?>");
			stringBuilder.Append("<root>");
			stringBuilder.Append("<Name>" + Name + "</Name>");
			stringBuilder.Append("<Type>" + Type + "</Type>");
			stringBuilder.Append("<LabelType>" + Type + "</LabelType>");
			stringBuilder.Append("<LabelIntro>" + Intro + "</LabelIntro>");
			stringBuilder.Append("<LabelDataType>" + DataType + "</LabelDataType>");
			stringBuilder.Append("<CacheTime>" + CacheTime + "</CacheTime>");
			stringBuilder.Append("<OutType>" + OutPutType + "</OutType>");
			stringBuilder.Append("<ConnStr>" + ConnStr + "</ConnStr>");
			stringBuilder.Append("<OutPutType>" + LabelOutPutType + "</OutPutType>");
			stringBuilder.Append("<StaticOutPut>" + LabelStaticOutPut + "</StaticOutPut>");
			stringBuilder.Append("<SqlStr>" + LabelSqlString + "</SqlStr>");
			stringBuilder.Append("<PageSqlStr>" + CountPageSqlStr + "</PageSqlStr>");
			if (!string.IsNullOrEmpty(CountPageSqlStr) && CountPageSqlStr != "")
			{
				stringBuilder.Append("<EnablePage>1</EnablePage>");
			}
			else
			{
				stringBuilder.Append("<EnablePage>0</EnablePage>");
			}
			stringBuilder.Append(ParmArray);
			stringBuilder.Append("</root>");
			return stringBuilder.ToString();
		}
		public string UpdateDefine(string DefineContent, string Name, string Type, string Intro, string DataType, string CacheTime, string OutPutType, string ConnStr, string LabelTemplate, string LabelSqlString, string EnablePage, string CountPageSqlStr, string LabelOutPutType, string LabelStaticOutPut, string[,] ParmArray)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(DefineContent);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("root");
			XmlNode xmlNode2 = xmlDocument.SelectSingleNode("root/Name");
			XmlNode xmlNode3 = xmlDocument.SelectSingleNode("root/Type");
			XmlNode xmlNode4 = xmlDocument.SelectSingleNode("root/LabelType");
			XmlNode xmlNode5 = xmlDocument.SelectSingleNode("root/LabelIntro");
			XmlNode xmlNode6 = xmlDocument.SelectSingleNode("root/LabelDataType");
			XmlNode xmlNode7 = xmlDocument.SelectSingleNode("root/CacheTime");
			XmlNode xmlNode8 = xmlDocument.SelectSingleNode("root/OutType");
			XmlNode xmlNode9 = xmlDocument.SelectSingleNode("root/ConnStr");
			XmlNode xmlNode10 = xmlDocument.SelectSingleNode("root/SqlStr");
			XmlNode xmlNode11 = xmlDocument.SelectSingleNode("root/PageSqlStr");
			XmlNode xmlNode12 = xmlDocument.SelectSingleNode("root/EnablePage");
			XmlNode xmlNode13 = xmlDocument.SelectSingleNode("root/OutPutType");
			XmlNode xmlNode14 = xmlDocument.SelectSingleNode("root/StaticOutPut");
			if (xmlNode2 != null && Name != "")
			{
				xmlNode2.InnerXml = Name;
			}
			if (xmlNode3 != null && Type != "")
			{
				xmlNode3.InnerXml = Type;
			}
			if (xmlNode4 != null && OutPutType != "")
			{
				xmlNode4.InnerXml = OutPutType;
			}
			if (xmlNode8 != null && OutPutType != "")
			{
				xmlNode8.InnerXml = OutPutType;
			}
			if (xmlNode5 != null && Intro != "")
			{
				xmlNode5.InnerXml = Intro;
			}
			if (xmlNode6 != null && DataType != "")
			{
				xmlNode6.InnerXml = DataType;
			}
			if (xmlNode7 != null && CacheTime != "")
			{
				xmlNode7.InnerXml = CacheTime;
			}
			if (xmlNode9 != null && ConnStr != "")
			{
				xmlNode9.InnerXml = ConnStr;
			}
			if (xmlNode10 != null && LabelSqlString != "")
			{
				xmlNode10.InnerXml = LabelSqlString;
			}
			if (xmlNode12 != null && EnablePage != "")
			{
				xmlNode12.InnerXml = EnablePage;
			}
			if (xmlNode13 != null)
			{
				if (LabelOutPutType != "")
				{
					xmlNode13.InnerXml = LabelOutPutType;
				}
			}
			else
			{
				XmlElement xmlElement = xmlDocument.CreateElement("OutPutType");
				if (LabelOutPutType != "")
				{
					xmlElement.InnerText = LabelOutPutType;
				}
				else
				{
					xmlElement.InnerText = "activity";
				}
				xmlNode.AppendChild(xmlElement);
			}
			if (xmlNode14 != null)
			{
				if (LabelStaticOutPut != "")
				{
					xmlNode14.InnerXml = LabelStaticOutPut;
				}
			}
			else
			{
				XmlElement xmlElement2 = xmlDocument.CreateElement("StaticOutPut");
				if (LabelStaticOutPut != "")
				{
					xmlElement2.InnerText = LabelStaticOutPut;
				}
				xmlNode.AppendChild(xmlElement2);
			}
			if (EnablePage != "" && xmlNode11 != null && CountPageSqlStr != "")
			{
				if (EnablePage == "1")
				{
					xmlNode11.InnerXml = CountPageSqlStr;
				}
				else
				{
					xmlNode11.InnerXml = "";
				}
			}
			if (ParmArray.Length > 0)
			{
				XmlNodeList xmlNodeList = xmlDocument.SelectNodes("root/parms");
				if (xmlNodeList != null)
				{
					foreach (XmlNode oldChild in xmlNodeList)
					{
						xmlNode.RemoveChild(oldChild);
					}
				}
				for (int i = 0; i < ParmArray.Length / 3; i++)
				{
					XmlElement xmlElement3 = xmlDocument.CreateElement("parms");
					XmlElement xmlElement4 = xmlDocument.CreateElement("name");
					xmlElement4.InnerText = ParmArray[i, 0].ToString();
					XmlElement xmlElement5 = xmlDocument.CreateElement("default");
					xmlElement5.InnerText = ParmArray[i, 1].ToString();
					XmlElement xmlElement6 = xmlDocument.CreateElement("intro");
					xmlElement6.InnerText = ParmArray[i, 2].ToString();
					xmlElement3.AppendChild(xmlElement4);
					xmlElement3.AppendChild(xmlElement5);
					xmlElement3.AppendChild(xmlElement6);
					xmlNode.AppendChild(xmlElement3);
				}
			}
			return xmlDocument.InnerXml;
		}
		public void CreateStaticLabel(string Label, string CreateFileName)
		{
			string text = Template.TransferLabelToHtml(Label, false);
			string text2 = string.Empty;
			string str = HttpContext.Current.Server.MapPath("~/") + "\\Label\\LabelOutPut\\";
			string[] array = CreateFileName.Split(new char[]
			{
				'.'
			});
			if (array.Length == 2)
			{
				text2 = array[1];
			}
			else
			{
				CreateFileName += ".html";
				text2 = "html";
			}
			FileIO fileIO = new FileIO();
			if (text2.ToLower() == "js")
			{
				text = text.Replace('\r'.ToString(), "");
				text = text.Replace('\n'.ToString(), "");
				text = "document.write(\"" + text.Replace("\"", "'") + "\");";
			}
			if (fileIO.CreateTemplateFile(text, str + CreateFileName))
			{
				HttpContext.Current.Response.Write("<script>alert('标签生成成功！');location.href='Admin_Lable.aspx'</script>");
				HttpContext.Current.Response.End();
			}
			else
			{
				HttpContext.Current.Response.Write("<script>alert('标签生成失败，失败原因：标签页已被其它进程访问或该标签没有写入权限！');history.goback();</script>");
				HttpContext.Current.Response.End();
			}
		}
		public void CreateStaticLabel(int LabelID)
		{
			if (LabelID > 0)
			{
				DataTable labelByLabelID = this.GetLabelByLabelID(LabelID);
				string text = string.Empty;
				string text2 = string.Empty;
				string a = string.Empty;
				if (labelByLabelID.Rows.Count == 1)
				{
					text = labelByLabelID.Rows[0]["StaticOutPut"].ToString();
					a = labelByLabelID.Rows[0]["OutPutType"].ToString();
					text2 = labelByLabelID.Rows[0]["Name"].ToString();
					if (text == null || text == "" || a != "static")
					{
						HttpContext.Current.Response.Write("<script>alert('此标签非静态标签，请重新设置再生成！');location.href='Admin_Lable.aspx'</script>");
						HttpContext.Current.Response.End();
					}
					text2 = "{HL.Label ID=\"" + text2 + "\" }";
					this.CreateStaticLabel(text2, text);
				}
				else
				{
					HttpContext.Current.Response.Write("<script>alert('标签不存在！');</script>");
					HttpContext.Current.Response.End();
				}
			}
		}
	}
}
